package reflect;

import jdk.internal.org.objectweb.asm.tree.analysis.Value;
import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class Test10 {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, InstantiationException, ClassNotFoundException, URISyntaxException, NoSuchMethodException {
        String pack = Test9.class.getPackage().getName();

        File dir = new File(
                Test9.class.getResource(".").toURI()
        );
        File[] suds = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File sud : suds) {
            String fileName = sud.getName();
            String className = fileName.replace(".class", "");
            Class cls = Class.forName(pack + "." + className);
            if (cls.isAnnotationPresent(AutoRunClass.class)) {
                System.out.println("实例化:" + className);
                Object obj = cls.newInstance();
                System.out.println(obj);
                Class cls1 = Class.forName("reflect.Person");
                Method method = cls1.getDeclaredMethod("sayHello");
                if (method.isAnnotationPresent(AutoRunMethod.class)) {
                    AutoRunMethod arm = method.getAnnotation(AutoRunMethod.class);
                    int value = arm.value();
                    System.out.println(value);

                    for (int i = 0; i <= value; i++) {
                        Method[] methods = cls.getDeclaredMethods();
                        for (Method metho : methods) {
                            if (method.isAnnotationPresent(AutoRunMethod.class)) {

                                System.out.println("执行方法:" + metho.getName() + "()");
                                method.invoke(obj);
                            }


                        }
                        System.out.println("第"+i+"次输出");
                    }
                }
            }
        }
    }
}
